﻿using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Linq;


namespace _1.OrderManage
{
    class OrderDetail
    {
        public string productName{ get; set; }
        public string customer { get; set; }
        public double orderAmount { get; set; }

        public override bool Equals(object obj)
        {
            OrderDetail other = obj as OrderDetail;
            return this.productName == other.productName && this.customer == other.customer && this.orderAmount == other.orderAmount;
        }
    }


    class Order
    {
        public Order(long orderID,string productName,string customer,double orderAmount)
        {
            this.orderID = orderID;
            orderDetail = new OrderDetail();
            orderDetail.productName = productName;
            orderDetail.customer = customer;
            orderDetail.orderAmount = orderAmount;
        }
        public long orderID { get; set; }
        public OrderDetail orderDetail;

        public override bool Equals(object obj)
        {
            Order order = obj as Order;
            return this.orderID == order.orderID || this.orderDetail.Equals(order.orderDetail);
        }

        public override string ToString()
        {
            return string.Format("{0,8} {1,10} {2,10} {3,10}", orderID.ToString(), orderDetail.productName, orderDetail.customer, orderDetail.orderAmount.ToString());
            //return string.Format("{0,8} {1,10} {2} {3,10}", orderID.ToString().PadLeft(8,' '), orderDetail.productName.PadLeft(10,' '), orderDetail.customer.PadLeft(10, ' '), orderDetail.orderAmount.ToString().PadLeft(14, ' '));
            //return orderID + " " + orderDetail.productName + " " + orderDetail.customer + " " + orderDetail.orderAmount;
        }


        public int CompareByOrderID(Order other)
        {
            return this.orderID.CompareTo(other.orderID);
        }


        public int CompareByCustomer(Order other)
        {
            return this.orderDetail.customer.CompareTo(other.orderDetail.customer);
        }

        public int CompareByProduct(Order other)
        {
            return this.orderDetail.productName.CompareTo(other.orderDetail.productName);
        }

        public int CompareByAmount(Order other)
        {
            return this.orderDetail.orderAmount.CompareTo(other.orderDetail.orderAmount);
        }


    }

    class OrderService
    {
        public OrderService()
        {
            orders = new List<Order>();
        }
        public List<Order> orders;


        public List<Order> queryByProduct(string productName)
        {
            List<Order> orderList = new List<Order>();
            for(int i = 0; i < orders.Count; ++i)
            {
                if (orders[i].orderDetail.productName == productName)
                {
                    orderList.Add(orders[i]);
                }
            }
            orderList.Sort((Order x, Order y)=>x.CompareByAmount(y));
            return orderList;
        }
        public bool addOrder(Order newOrder)
        {
            for(int i = 0; i < orders.Count; ++i)
            {
                if (orders[i].Equals(newOrder)) return false;
            }
            orders.Add(newOrder);
            orders.Sort((x,y)=>x.CompareByOrderID(y));
            return true;
        }

        public bool deleteOrder(long orderID)
        {
            for(int i = 0; i < orders.Count; ++i)
            {
                if (orders[i].orderID == orderID)
                {
                    orders.Remove(orders[i]);
                    return true;
                }
            }
            return false;
        }


        public bool isHaveOrder(long orderID)
        {
            for(int i = 0; i < orders.Count; ++i)
            {
                if (orders[i].orderID == orderID)
                {
                    return true;
                }
            }
            return false;
        }

        public bool modifyOrder(long orderID, string productName, string customer, double orderAmount)
        {
            for (int i = 0; i < orders.Count; ++i)
            {
                if (orders[i].orderID == orderID)
                {
                    orders[i].orderDetail.productName = productName;
                    orders[i].orderDetail.customer = customer;
                    orders[i].orderDetail.orderAmount = orderAmount;
                    return true;
                }
            }
            return false;
        }

    }

    class Program
    {
        static bool isCorrect(string operateNum,List<string>nums)
        {
            if (operateNum.Length >= 2) return false;
            if (nums.Find((x)=>x==operateNum)==null) return false;
            return true;
        }

        static void query(string queryNum, OrderService orderService)
        {
            if (queryNum == "1")
            {
                Console.WriteLine(" 订单单号     商品名称     客户名称      订单金额 ");
                List<Order> orders = orderService.orders;
                for (int i = 0; i < orders.Count; ++i) Console.WriteLine(orders[i]);
            }
            else if (queryNum == "2")
            {
                Console.WriteLine("请输入查询的订单单号：");
                string orderIDstr = Console.ReadLine();
                while (!IsNumber(orderIDstr))
                {
                    Console.WriteLine("输入不合法，请重新输入");
                    orderIDstr = Console.ReadLine();
                }
                long orderID = Convert.ToInt64(orderIDstr);
                var queryAns = from order in orderService.orders where order.orderID == orderID orderby order.orderDetail.orderAmount select order;
                Console.WriteLine(" 订单单号     商品名称     客户名称      订单金额 ");
                foreach (Order order in queryAns)
                { 
                    Console.WriteLine(order);
                }
            }
            else if(queryNum == "3")
            {
                Console.WriteLine("请输入查询的商品名称：");
                string productName = Console.ReadLine();
                var queryAns = from order in orderService.orders where order.orderDetail.productName == productName orderby order.orderDetail.orderAmount select order;
                Console.WriteLine(" 订单单号     商品名称     客户名称      订单金额 ");
                foreach (Order order in queryAns)
                { 
                    Console.WriteLine(order);
                }
            }
            else if (queryNum == "4")
            {
                Console.WriteLine("请输入查询的客户名称：");
                string customer = Console.ReadLine();
                var queryAns = from order in orderService.orders where order.orderDetail.customer == customer orderby order.orderDetail.orderAmount select order;
                Console.WriteLine(" 订单单号     商品名称     客户名称      订单金额 ");
                foreach (Order order in queryAns)
                {  
                    Console.WriteLine(order);
                }
            }else if (queryNum == "5")
            {
                Console.WriteLine("请输入查询的商品金额：");
                string orderAmountStr = Console.ReadLine();
                while (!IsAllNumber(orderAmountStr))
                {
                    Console.WriteLine("输入不合法，请重新输入");
                    orderAmountStr = Console.ReadLine();
                }
                double orderAmount = double.Parse(orderAmountStr);
                var queryAns = from order in orderService.orders where order.orderDetail.orderAmount == orderAmount orderby order.orderDetail.orderAmount select order;
                Console.WriteLine(" 订单单号     商品名称     客户名称      订单金额 ");
                foreach (Order order in queryAns)
                {
                    Console.WriteLine(order);
                }
            }
        }


        static bool IsNumber(string s)
        {
            if (string.IsNullOrWhiteSpace(s)) return false;
            const string pattern = "^[0-9]*$";
            Regex rx = new Regex(pattern);
            return rx.IsMatch(s);
        }

        //判断是不是实数
        static bool IsAllNumber(string text)
        {
            Regex objNotNumberPattern = new Regex("[^0-9.-]");
            Regex objTwoDotPattern = new Regex("[0-9]*[.][0-9]*[.][0-9]*");
            Regex objTwoMinusPattern = new Regex("[0-9]*[-][0-9]*[-][0-9]*");
            String strValidRealPattern = "^([-]|[.]|[-.]|[0-9])[0-9]*[.]*[0-9]+$";
            String strValidIntegerPattern = "^([-]|[0-9])[0-9]*$";
            Regex objNumberPattern = new Regex("(" + strValidRealPattern + ")|(" + strValidIntegerPattern + ")");
            return !objNotNumberPattern.IsMatch(text) && !objTwoDotPattern.IsMatch(text) && !objTwoMinusPattern.IsMatch(text) && objNumberPattern.IsMatch(text);
        }

        static void Main(string[] args)
        {
            OrderService orderService = new OrderService();
            Console.WriteLine("为了便于测试，事先导入了一些订单");
            Console.WriteLine();
            Order t1 = new Order(1, "苹果", "陈伟", 6.5);
            Order t2 = new Order(2, "猕猴桃", "陈伟", 11.4);
            Order t3 = new Order(3, "哈密瓜", "陈伟", 3.4);
            Order t4 = new Order(4, "苹果", "陈佩鑫", 6.9);
            Order t5 = new Order(5, "猕猴桃", "陈佩鑫", 12.4);
            Order t6 = new Order(6, "哈密瓜", "陈佩鑫", 4.4);
            orderService.addOrder(t1);
            orderService.addOrder(t2);
            orderService.addOrder(t3);
            orderService.addOrder(t4);
            orderService.addOrder(t5);
            orderService.addOrder(t6);


            while (true)
            {
                Console.WriteLine("################################");
                Console.WriteLine("## 请输入序号选择您的操作：#####");
                Console.WriteLine("## 1.查询订单   ################");
                Console.WriteLine("## 2.添加订单   ################");
                Console.WriteLine("## 3.删除订单   ################");
                Console.WriteLine("## 4.修改订单   ################");
                Console.WriteLine("## 5.修改排序方式###############");
                Console.WriteLine("## 6.退出       ################");
                Console.WriteLine("################################");
                string operateNum = Console.ReadLine();
                List<string> nums = new List<string> { "1", "2", "3", "4", "5", "6"};
                
                while (!isCorrect(operateNum,nums))
                {
                    Console.WriteLine("输入不合法，请重新输入");
                    operateNum = Console.ReadLine();
                }

                if (operateNum == "6") break;

                if (operateNum == "1")
                {
                    Console.WriteLine("################################");
                    Console.WriteLine("## 请输入序号选择您的查询：#####");
                    Console.WriteLine("## 1.查询全部订单          #####");
                    Console.WriteLine("## 2.按订单号查询          #####");
                    Console.WriteLine("## 3.按商品查询            #####");
                    Console.WriteLine("## 4.按客户查询            #####");
                    Console.WriteLine("## 5.按金额查询            #####");
                    Console.WriteLine("## 6.返回                  #####");
                    Console.WriteLine("################################");
                    List<string> queryNums = new List<string> { "1", "2", "3", "4", "5", "6" };
                    string queryNum = Console.ReadLine();
                    while (!isCorrect(queryNum, queryNums))
                    {
                        Console.WriteLine("输入不合法，请重新输入");
                        queryNum = Console.ReadLine();
                    }
                    query(queryNum,orderService);
                }
                else if(operateNum=="2")
                {
                    Console.WriteLine("请输入添加的订单单号：");
                    string orderIDstr = Console.ReadLine();
                    while (!IsNumber(orderIDstr))
                    {
                        Console.WriteLine("输入不合法，请重新输入");
                        orderIDstr = Console.ReadLine();
                    }
                    long orderID = Convert.ToInt64(orderIDstr);
                    Console.WriteLine("请输入添加商品的名称：");
                    string productName = Console.ReadLine();
                    Console.WriteLine("请输入添加的客户名称：");
                    string customer = Console.ReadLine();
                    Console.WriteLine("请输入添加的商品金额：");
                    string orderAmountStr = Console.ReadLine();
                    while (!IsAllNumber(orderAmountStr))
                    {
                        Console.WriteLine("输入不合法，请重新输入");
                        orderAmountStr = Console.ReadLine();
                    }
                    double orderAmount = double.Parse(orderAmountStr);
                    Order order = new Order(orderID, productName, customer, orderAmount);
                    if (orderService.addOrder(order)) Console.WriteLine("添加成功！！！！！！！！");
                    else Console.WriteLine("该订单已经存在，添加失败！！！！！！");
                }else if(operateNum=="3")
                {
                    Console.WriteLine("请输入要删除的订单单号：");
                    string orderIDstr = Console.ReadLine();
                    while (!IsNumber(orderIDstr))
                    {
                        Console.WriteLine("输入不合法，请重新输入");
                        orderIDstr = Console.ReadLine();
                    }
                    long orderID = Convert.ToInt64(orderIDstr);
                    if (orderService.deleteOrder(orderID)) Console.WriteLine("删除成功！！！！！！！！！！");
                    else Console.WriteLine("删除失败，该订单不存在！！！！！！！");
                }
                else if(operateNum=="4")
                {
                    Console.WriteLine("请输入要修改的订单单号：");
                    string orderIDstr = Console.ReadLine();
                    while (!IsNumber(orderIDstr))
                    {
                        Console.WriteLine("输入不合法，请重新输入");
                        orderIDstr = Console.ReadLine();
                    }
                    long orderID = Convert.ToInt64(orderIDstr);
                    if (!orderService.isHaveOrder(orderID))Console.WriteLine("修改失败，该订单不存在！！！！！！！");
                    else
                    {
                        Console.WriteLine("请输入修改后商品的名称：");
                        string productName = Console.ReadLine();
                        Console.WriteLine("请输入修改后的客户名称：");
                        string customer = Console.ReadLine();
                        Console.WriteLine("请输入修改后的商品金额：");
                        string orderAmountStr = Console.ReadLine();
                        while (!IsAllNumber(orderAmountStr))
                        {
                            Console.WriteLine("输入不合法，请重新输入");
                            orderAmountStr = Console.ReadLine();
                        }
                        double orderAmount = double.Parse(orderAmountStr);
                        if (orderService.modifyOrder(orderID, productName, customer, orderAmount)) Console.WriteLine("修改成功！！！！！！！！！！");
                        else Console.WriteLine("修改失败，该订单不存在！！！！！！！");
                    }
                }
                else if(operateNum=="5")
                {
                    Console.WriteLine("################################");
                    Console.WriteLine("## 请输入序号选择您的排序方式：#");
                    Console.WriteLine("## 1.按订单号排序          #####");
                    Console.WriteLine("## 2.按商品名称排序        #####");
                    Console.WriteLine("## 3.按客户名称排序        #####");
                    Console.WriteLine("## 4.按金额排序            #####");
                    Console.WriteLine("################################");
                    List<string> sortNums = new List<string> { "1", "2", "3", "4"};
                    string sortNum = Console.ReadLine();
                    while (!isCorrect(sortNum, sortNums))
                    {
                        Console.WriteLine("输入不合法，请重新输入");
                        sortNum = Console.ReadLine();
                    }
                    if (sortNum == "1")
                    {
                        orderService.orders.Sort((x, y) =>x.CompareByOrderID(y));
                    }else if (sortNum == "2")
                    {
                        orderService.orders.Sort((x, y) => x.CompareByProduct(y));
                    }
                    else if (sortNum == "3")
                    {
                        orderService.orders.Sort((x, y) => x.CompareByCustomer(y));
                    }
                    else if (sortNum == "4")
                    {
                        orderService.orders.Sort((x, y) => x.CompareByAmount(y));
                    }
                    Console.WriteLine("排序成功！！！！！！！！");
                }

                Console.WriteLine();
                Console.WriteLine();
                //Console.Clear();
            }
        }
    }
}
